
var MIND_EDIT_JOB = "/livcos.org/MindNavi/web/edit.job";

function ShowEditAction(entity) {
  this.id = "showEdit";
  this.label = "Edit...";
  this.entity = entity;
}

ShowEditAction.prototype.perform = function() {
  document.editorPanel.open();
}


function ShowInsertAction() {
  this.id = "showInsert";
  this.label = "Insert...";
  this.panel = document.insertPanel;
  if (!this.panel) {
    this.panel = Dom("_insert");
    document.insertPanel = this.panel;
    this.panel._disable = Dom("_disable");
    this.panel._disable.setOpacity(0.8);
    this.panel._disable.dom._panel = this.panel;
    new EventListener(this.panel._disable.dom, "click", function(evn) {
      this._panel.hide();
      this._panel._disable.hide();
      return EventListener.cancelEvent(evn);
    });
	  var btn = this.panel.dom.getElementsByTagName("input")[1];
	  btn._panel = this.panel;
    var input = this.panel.dom.getElementsByTagName("input")[0];
    input._panel = this.panel;
    this.panel._input = input;
    this.panel._ac = new AutoCompleteInput(this.panel._input, document._nav.list);
    var form = this.panel.dom.firstChild;
    form._panel = this.panel;
    new EventListener(form, "submit", function(evn) {
      var a = new CreateAction(this._panel._input.value);
      a.perform();
      this._panel.hide();
      this._panel._disable.hide();
      return EventListener.cancelEvent(evn);
    });
  }
}

ShowInsertAction.prototype.perform = function() {
	var nav = document._nav;
	if (nav.list.length <= 0) {
		var mindSet = nav.current.id;
		mindSet = mindSet.substring(0, mindSet.lastIndexOf("/"));
    var r = new Resource("list.view?set="+mindSet, "", StatusDisplay.getInstance());
    r._nav = document._nav;
    r.onCompleted = function() {
      eval("var l = "+this.text);
      for (var i = 0; i < l.length; ++i)
        this._nav.list[i] = l[i];
      StatusDisplay.getInstance().show("...list loaded.", null, true);
    }
    r.load();
	}
  this.panel.show();
  this.panel._disable.show();
}

ShowInsertAction.prototype.close = function() {
  document.unselectEntityView();
  this.panel._input.focus && this.panel._input.focus();
  this.panel._input.select && this.panel._input.select();
}

function CreateAction(labelValue) {
  this.id = "create";
  this.label = "Create";
  this.nav = document._nav;
  this.labelValue = labelValue;
}

CreateAction.prototype.perform = function() {
  var editJob = new Job(BasePath.get()+MIND_EDIT_JOB);
  editJob._nav = this.nav;
  editJob.addData("thgt", this.nav.current.id);
  editJob.addData("label", this.labelValue);
  editJob.addData("resp", this.nav.current.id);
  editJob.addData(this.id, "1");
  editJob.onSuccess = function() {
    eval("var c = "+this.text);
    var t = this._nav.createThought(c);
    t.init(c);
    this._nav.navTo(t, true);
  }
  editJob.start();
}

function UpdateAction(form) {
  this.id = "update";
  this.label = "Update";
  this.form = form;
  this.reset();
}

UpdateAction.prototype.perform = function() {
  this.state = "running";
  var editJob = new Job(BasePath.get()+MIND_EDIT_JOB);
  editJob._nav = document._nav;
  editJob.addData(this.id, "1");
  editJob.addData("resp", document._nav.current.id)
  editJob.addFormData(this.form);
  editJob.onSuccess = function() {
    eval("var c = "+this.text);
    var t = this._nav.createThought(c);
    t.init(c);
    this._nav.navTo(t, true);
  }
  editJob.start();
  document.currentSelection.process();
}

UpdateAction.prototype.close = function() {
  document.unselectEntityView();
  this.state = "closed";
}

UpdateAction.prototype.reset = function() {
  this.state = "initial";
}


function UpdateNoteAction(value) {
  this.id = "update";
  this.label = "Update";
  this.value = value;
}

UpdateNoteAction.prototype.perform = function() {
  var editJob = new Job(BasePath.get()+MIND_EDIT_JOB);
  editJob._nav = document._nav;
  editJob.addData(this.id, "1");
  editJob.addData("objId", document._nav.current.id)
  editJob.addData("resp", document._nav.current.id)
  editJob.addData("ns-m", "livcos.org/ns/mind");
  editJob.addData("path", "m:note");
  editJob.addData("content", this.value.replace("\n", "<br/>"));
  editJob.onSuccess = function() {
    eval("var c = "+this.text);
    var t = this._nav.createThought(c);
    t.init(c);
    this._nav.navTo(t, true);
  }
  editJob.start();
}

UpdateNoteAction.prototype.close = function() {
}


function DeleteAction(entity) {
  this.id = "delete";
  this.label = "Delete";
  this.entity = entity;
  this.nav = document._nav;
}

DeleteAction.prototype.perform = function() {
	var editJob = new Job(BasePath.get()+MIND_EDIT_JOB);
	editJob._nav = this.nav;
  editJob.addData("thgt", this.entity.objId);
  editJob.addData("rel", this.entity.id);
  editJob.addData("resp", this.nav.current.id);
  editJob.addData(this.id, "1");
  editJob.onSuccess = function() {
  	eval("var c = "+this.text);
  	var t = this._nav.createThought(c);
  	t.init(c);
  	this._nav.navTo(t, true);
  }
	editJob.start();
	document.currentSelection.process();
}

DeleteAction.prototype.close = function() {
	document.unselectEntityView();
}

